/*
 * FoodPanel.java
 *
 * Created on 29. leden 2009, 2:25
 */

package jmenu.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import jmenu.dbmodels.MFood;
import jmenu.dbmodels.MFoods;
import jmenu.main.Config;
import jmenu.main.Icons;
import jmenu.main.MyDefaultTableCellPictureRenderer;
import jmenu.main.MyDefaultTableCellRenderer;
import jmenu.main.MyDefaultTableHeaderRenderer;

/**
 *
 * @author  Roman
 */
public class FoodPanel extends javax.swing.JPanel {
    
    private DefaultTableModel model;
    
    /** Creates new form FoodPanel */
    public FoodPanel()
    {
        initComponents();
    }

    public FoodPanel(MFoods foods)
    {
        initComponents();
        this.addRow(foods);
    }
    
    public void init()
    {
       this.model = new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Potraviny", "Bílkoviny (g)", "Sacharidy (g)", "Tuky (g)", "Energie (KJ)", "Cholesterol (mg)", "Obrázek", "Obrázek(Id)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Object.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false,false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
          };
        foodTable.setModel(model);
        this.foodTable.setRowHeight(25);
        


        foodTable.setShowHorizontalLines(false);
        foodTable.setShowVerticalLines(false);
        foodTable.setIntercellSpacing(new Dimension(0, 0));

        MyDefaultTableHeaderRenderer header = new MyDefaultTableHeaderRenderer();
        header.setHorizontalAlignment(JLabel.CENTER);

        for(int i=0; i< foodTable.getColumnCount(); i++)
            foodTable.getColumnModel().getColumn(i).setHeaderRenderer(header);

        MyDefaultTableCellRenderer cell = new MyDefaultTableCellRenderer();
        cell.setHorizontalAlignment(JLabel.CENTER);

        for(int i=0; i< foodTable.getColumnCount(); i++)
            foodTable.getColumnModel().getColumn(i).setCellRenderer(cell);


        cell = new MyDefaultTableCellRenderer();
        cell.setHorizontalAlignment(JLabel.LEFT);

        foodTable.getColumnModel().getColumn(1).setCellRenderer(cell);
        foodTable.getColumnModel().getColumn(1).setPreferredWidth(190);

        MyDefaultTableCellPictureRenderer picture= new MyDefaultTableCellPictureRenderer();
        picture = new MyDefaultTableCellPictureRenderer();
        picture.setHorizontalAlignment(JLabel.CENTER);
        foodTable.getColumnModel().getColumn(7).setCellRenderer(picture);
        foodTable.getColumnModel().getColumn(7).setPreferredWidth(25);

    }
    
    public void addRow(MFood row)
    {
        if (row == null)
            return;

        this.model.addRow(new Object[]{row.getId(),
                                       row.getName(),
                                       row.getProtein(),
                                       row.getCarbohydrate(),
                                       row.getFat(),
                                       row.getEnergy(),
                                       row.getCholesterol(),
                                       row.getPictureId(),
                                       row.getPictureId()
                                          });
    }
    
    public void addRow(MFoods rows)
    {
        if (rows != null)
            if (rows.getRows().size() > 0)
                for (int k=0; k < rows.getRows().size(); k++)
                {
                    this.addRow(rows.getRows().get(k));
                }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        foodTable = new javax.swing.JTable();

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        foodTable.setAutoCreateRowSorter(true);
        foodTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Potraviny", "Bílkoviny", "Sacharidy", "Tuky", "Energie", "Cholesterol", "Obrázek"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        init();
        foodTable.setName("foodTable"); // NOI18N
        foodTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        foodTable.getTableHeader().setReorderingAllowed(false);
        foodTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                foodTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(foodTable);
        foodTable.getColumnModel().getColumn(0).setResizable(false);
        foodTable.getColumnModel().getColumn(1).setResizable(false);
        foodTable.getColumnModel().getColumn(2).setResizable(false);
        foodTable.getColumnModel().getColumn(3).setResizable(false);
        foodTable.getColumnModel().getColumn(4).setResizable(false);
        foodTable.getColumnModel().getColumn(5).setResizable(false);
        foodTable.getColumnModel().getColumn(6).setResizable(false);
        foodTable.getColumnModel().getColumn(7).setResizable(false);
        foodTable.removeColumn(foodTable.getColumnModel().getColumn(0));
        foodTable.removeColumn(foodTable.getColumnModel().getColumn(7));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void foodTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_foodTableMouseClicked

    if (evt.getButton() == MouseEvent.BUTTON1)
    {
        int selectedColumn = foodTable.getSelectedColumn();
        int selectedRow = foodTable.getSelectedRow();

        if (evt.getClickCount() == 2)
        {
            if (selectedColumn == 6)
            {
                if (!String.valueOf(model.getValueAt(foodTable.getSelectedRow(), 7)).equals("-1"))
                {
                    ShowPictureDialog dlg = new ShowPictureDialog(null,true);
                    int id = Integer.parseInt(String.valueOf(model.getValueAt(selectedRow, 7)));
                    dlg.setImageIdAndText(id, String.valueOf(model.getValueAt(selectedRow, 1)));
                    dlg.setLocationRelativeTo(this);
                    dlg.setVisible(true);
                }
            }
            System.out.println(model.getValueAt(foodTable.getSelectedRow(), 0)+"a spoupec");

        }
    }
}//GEN-LAST:event_foodTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable foodTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
